﻿using UnityEngine;
using System.Collections;

namespace View
{
    public class SmokeParticle
    {

        private int seed;
        private float radius;
        private Vector2 startPos;
        private Vector2 position;
        private Vector2 velocity;
        private Vector2 acceleration;
        private float lifeTime;
        private float maxLifeTime;
        private Color fadeColor;
        private Color previousColor;

        private float maxRadius = 1f;
        private float minRadius = 0.1f;
        private float delayTimeSeconds;

        private float ratio;

        public SmokeParticle(int seed, Vector2 startPos, float ratio)
        {
            fadeColor = GUI.color;
            previousColor = GUI.color;
            this.minRadius *= ratio;
            this.maxRadius *= ratio;
            this.ratio = ratio;

            this.seed = seed;
            this.startPos = startPos;

            this.acceleration = new Vector2(0.0f * ratio, -1.0f * ratio);

            setUpParticle();
        }

        private void setUpParticle()
        {
            lifeTime = 0;
            maxLifeTime = 3;

            this.position = new Vector2(startPos.x, startPos.y);

            System.Random rand = new System.Random(this.seed);
            this.velocity = new Vector2(((float)rand.NextDouble() - 0.5f) * ratio, ((float)rand.NextDouble() - 0.5f));
            // normalize to get it spherical vector with length 1.0
            this.velocity.Normalize();
            // add random length between 0 to maxSpeed
            float maxSpeed = 0.5f * ratio;
            float minSpeed = 0.1f * ratio;
            this.velocity *= minSpeed + (float)rand.NextDouble() * (maxSpeed - minSpeed);

            delayTimeSeconds = (float)(rand.NextDouble() * maxLifeTime);
        }

        private void interpolation()
        {
            float t = getTimeLivedInSeconds() / maxLifeTime;

            if (t > 1.0f)
            {
                t = 1.0f;
                //setUpParticle();
            }

            float startValue = 1.0f;
            float endValue = 0.0f;

            radius = minRadius + t * maxRadius;
            fadeColor.a = (endValue * t + (1.0f - t) * startValue);
        }

        // Update is called once per frame
        public void Update()
        {
            lifeTime += Time.deltaTime;

            if (lifeTime > delayTimeSeconds)
            {
                velocity.x = Time.deltaTime * acceleration.x + velocity.x;
                velocity.y = Time.deltaTime * acceleration.y + velocity.y;

                position.x += Time.deltaTime * velocity.x;
                position.y += Time.deltaTime * velocity.y;
            }
        }

        public void OnGUI(Texture2D texture, GameCamera cam)
        {
            if (isAlive())
            {
                interpolation();

                Vector2 visualPos = cam.getVisualCoordinates(position);
                Vector2 visualSize = cam.getVisualSizeWithRadius(radius);

                GUI.color = fadeColor;
                GUI.DrawTexture(new Rect(visualPos.x - visualSize.x / 2, visualPos.y - visualSize.y / 2, visualSize.x, visualSize.y), texture);
                GUI.color = previousColor;
            }
        }

        private float getTimeLivedInSeconds()
        {
            if (isAlive())
            {
                return lifeTime - delayTimeSeconds;
            }
            return 0;
        }

        private bool isAlive()
        {
            return lifeTime > delayTimeSeconds;
        }
    }
}